$(function () {
  getCate()
  form.on('submit(formDemo)', function (data) {
    axios.post('/my/article/addcates', `name=${data.field.name}&alias=${data.field.alias}`)
      .then(res => {
        if (res.data.status !== 0) {
          layer.msg('添加失败')
        }
        layer.closeAll();
        getCate()
      })
    return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
  });

})
function getCate() {
  let tbody = document.querySelector('tbody')
  axios.get('/my/article/cates')
    .then(res => {
      let data = res.data
      tbody.innerHTML = template('tpl-table', data)
      // 删除
      let btn_delete = document.querySelectorAll('.btn-delete')
      btn_delete.forEach(item => {
        item.addEventListener('click', function () {
          let id = this.getAttribute('data-id')
          axios.get(`/my/article/deletecate/${id}`)
            .then(res => {
              getCate()
            })
        })
      })
      // 编辑
      let btn_edit = document.querySelectorAll('.btn-edit')
      btn_edit.forEach(item => {
        item.addEventListener('click', function () {
          let dialog_edit = document.querySelector('#dialog-edit')
          layer.open({
            type: 1,
            title: '文章类别修改',
            content: dialog_edit.innerHTML//这里content是一个普通的String
          });
          let Id = this.getAttribute('data-id')
          form.on('submit(formDemo1)', function (data) {
            axios.post('/my/article/updatecate', `Id=${Id}&name=${data.field.name}&alias=${data.field.alias}`)
              .then(res => {
                layer.closeAll();
                getCate()
              })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
          });
        })
      })
    })
  let btnAddCate = document.querySelector('#btnAddCate')
  let dialog_add = document.querySelector('#dialog-add')
  btnAddCate.addEventListener('click', function () {
    layer.open({
      type: 1,
      title: '文章类别添加',
      content: dialog_add.innerHTML//这里content是一个普通的String
    });

  })
}
